# -*- coding: utf-8 -*-

import unittest
import os.path
import mox

from model.gamestate import GameState, Phase, Players, Player, Word, Settings
from presenter_mock import PresenterMock
from start_game import StartGame
from model.json_loader import JsonLoader


class StartGameTest(unittest.TestCase):
    def setUp(self):
        self.mox = mox.Mox()
        player1 = Player('Vasya', [])
        player2 = Player('Kostya', [])
        word1 = Word(player1, 'Word1')
        word2 = Word(player2, 'Word2')
        player1.add_word(word1)
        player2.add_word(word2)
        players1 = Players([player1, player2])
        players2 = Players([Player('Vasya', [])])
        players3 = Players([Player('Vasya', []), Player('Kostya', [])])
        settings = Settings()
        self.game_state1 = GameState(players1, None, None, 0, [], None,
                                     settings)
        self.game_state2 = GameState(players2, None, None, 0, [], None, None)
        self.game_state3 = GameState(players3, None, None, 0, [], None, None)

    def test_add_player_button(self):
        self.presenter = PresenterMock(Phase.ADD_PLAYER)
        widget = StartGame(self.game_state1, self.presenter)
        widget.add_player()
        self.assertEquals(self.presenter.is_phase_changed, True)

    def test_start_game_button(self):
        self.presenter = PresenterMock(Phase.PLAYERS_ORDER)
        widget = StartGame(self.game_state1, self.presenter)
        widget.start_game()
        self.assertEquals(self.presenter.is_phase_changed, True)

    def test_make_joinable_button(self):
        self.presenter = PresenterMock(Phase.START_GAME)
        widget = StartGame(self.game_state1, self.presenter)
        m = mox.Mox()
        m.StubOutWithMock(JsonLoader, 'load_from_url')
        JsonLoader.load_from_url(mox.IgnoreArg()).AndReturn({u'id': 0,
                                                             u'pin': 0})
        JsonLoader.load_from_url(mox.IgnoreArg()).AndReturn({u'players': [],
                                                             u'hat': []})
        m.ReplayAll()
        self.assertEquals(widget.join_btn.text, _('Host game'))
        widget.join_to_game()
        m.VerifyAll()
        self.assertEquals(widget.join_btn.text, _('Update'))

    def test_go_back_button(self):
        self.presenter = PresenterMock(Phase.GAME_CONFIG)
        widget = StartGame(self.game_state1, self.presenter)
        widget.go_back()
        self.assertEquals(self.presenter.is_phase_changed, True)

    def test_start_game(self):
        self.presenter = PresenterMock(Phase.PLAYERS_ORDER)
        widget = StartGame(self.game_state2, self.presenter)
        widget.start_game()
        self.assertEquals(self.presenter.is_phase_changed, False)
        widget = StartGame(self.game_state3, self.presenter)
        widget.start_game()
        self.assertEquals(self.presenter.is_phase_changed, True)

    def test_update(self):
        self.presenter = PresenterMock(Phase.START_GAME)
        widget = StartGame(self.game_state1, self.presenter)
        widget.update()
        self.assertEquals(widget.start_btn.enabled, True)
        widget = StartGame(self.game_state2, self.presenter)
        widget.update()
        self.assertEquals(widget.start_btn.enabled, False)
        widget = StartGame(self.game_state3, self.presenter)
        widget.update()
        self.assertEquals(widget.start_btn.enabled, True)

    def test_load_players(self):
        self.presenter = PresenterMock(Phase.START_GAME)
        widget = StartGame(self.game_state1, self.presenter)
        widget.load_players()

    def test_send_player(self):
        self.presenter = PresenterMock(Phase.START_GAME)
        widget = StartGame(self.game_state1, self.presenter)
        widget.send_player(self.game_state1.players.players[0])


if __name__ == '__main__':
    unittest.main()
